package org.example.controller;


import org.example.entity.MyConsult;
import org.example.service.MyConsultService;
import org.example.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName MyConsultController
 * @Description TODO
 * @Author qiuye
 * @Date 2025年08月15日 15:56
 * @Version 1.0
 */
@RestController
@RequestMapping("myconsult")
public class MyConsultController {
    @Autowired
    private MyConsultService myConsultService;
    @GetMapping("{id}")
    public R getMyConsult(@PathVariable("id") int id) {
        List<MyConsult> myConsult = myConsultService.getMyConsult(id);
        return R.ok(myConsult);
    }

    @PostMapping("/add")
    @Transactional
    public R addConsult(@RequestBody MyConsult consult) {
        try {
            // 设置默认值（如果前端没传）
            if (consult.getStatus() == null) consult.setStatus("未完成");
            if (consult.getAssessStatus() == null) consult.setAssessStatus("未评价");

            myConsultService.addMyConsult(consult); // 保存到数据库
            return R.ok("咨询提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("提交失败：" + e.getMessage());
        }
    }

    @DeleteMapping("{id}")
    public R deleteMyConsult(@PathVariable("id") int id) {
        int i = myConsultService.deleteMyConsult(id);
        if (i == 0){
            return R.fail();
        }
        return R.ok(i);
    }
    @PutMapping("/myconsult") // 确保URL与前端请求一致
    public R updataMyConsult(@RequestBody MyConsult myConsult) {
        int i = myConsultService.updateMyConsult(myConsult);
        if (i == 0){
            return R.fail();
        }
        return R.ok(i);
    }
}
